<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <VGroup>
          <el-select style="max-width: 100px" class="selectClass" v-model="form.personRange.type" placeholder="请选择">
            <el-option v-for="item in personOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <select-personnel class="w-40" v-model="form.personRange.value"></select-personnel>
        </VGroup>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()"
            style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="open('add')"><svg-icon class="text-white mr-1" icon-class="add" />新增</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" @query="query"></ComModal>
    <noticeModal ref="noticeRef"></noticeModal>

  </VPage>
</template>
<script setup name="SystemNotice" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { nextTick, ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryNoticeList } from './api'
import { getWareData } from '@/api/public'
import ComModal from './components/ComModal.vue'
import noticeModal from '../../home/index/components/noticeModal.vue'

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "修改时间", value: "update_time" },
]

const personOptions = [
  { label: "创建人", value: "create_user_id" },
  { label: "修改人", value: "update_user_id" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      personRange: { type: 'create_user_id', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      field: 'title', title: '标题', width: 300
    },
    {
      field: 'create_user', title: '创建人'
    },
    {
      field: 'create_time', title: '创建日期'
    },
    {
      field: 'update_user', title: '最近修改人'
    },
    {
      field: 'update_time', title: '修改日期'
    },
    {
      title: '操作', width: "100", fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => open('edit', row.id)}>修改</el-link>
              <el-link type="primary" underline={false} class="mr-2" onClick={() => openNotice('view', row.id)}>查看</el-link>

            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryNoticeList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
  wareDataList: [],
  floorList: [],
  areaList: []
})
const { options, wareDataList, floorList, areaList } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "system.pickerManage"
    ]
  }).then(res => {
    options.value = res.data
  })
}
provide('options', options)
fetchOption()

// 获取仓库
const getWareDataList = async () => {
  try {
    const res = await getWareData()
    wareDataList.value = res.data
  } catch (e) {
    throw Error(e)
  }
}
provide('wareDataList', wareDataList)
getWareDataList()


const changeWarehouse = async (val) => {
  console.log(val)
  let floors = []
  wareDataList.value.forEach(element => {
    if (val && val.includes(element.id)) {
      floors.push(...element.floors)
    }
  });
  floorList.value = floors
  areaList.value = []
  tableRef?.value.setFormField('floor_id', '')
  tableRef?.value.setFormField('area_id', '')
}

const changeFloor = async (val) => {
  let areas = []
  floorList.value.forEach(element => {
    if (val && val.includes(element.id)) {
      areas.push(...element.areas)
    }
  });
  areaList.value = areas
  tableRef?.value.setFormField('area_id', '')
}

const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

const noticeRef = ref()
const openNotice = (type, id) => {
  noticeRef.value.open(type, id)
}
</script>